package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import logging.Logger;

import util.Settings;

public class SettingsPanel extends JFrame {
	private static final long serialVersionUID = -6012085145032790427L;

	private SettingsPanel thisSettingsPanel = this;
	
	private JTextField numNodesTBox;
	private JTextField seedTBox;
	
	private JTextField randConnNumTBox;
	public SettingsPanel(){
		Rectangle usableRect = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
		this.setMaximumSize(new Dimension(usableRect.width, usableRect.height));
		this.setLayout(new GridBagLayout());
		JTabbedPane tabbedPane = new JTabbedPane();
		GridBagConstraints  gbc = new GridBagConstraints();
		gbc.gridwidth = 4;
		gbc.gridx = 0;
		gbc.gridy = 0;
		this.add(tabbedPane, gbc);
		this.generalSettingsPanel(tabbedPane);
		this.randSettingsPanel(tabbedPane);
		JButton submit = new JButton("Submit");
		JButton reset = new JButton("Reset");
		submit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Logger.getInstance().log(Logger.TRACE, "SettingsPanel.submit click");
				Settings settings = Settings.getInstance();
				try{
					settings.setNumRandomConnections(Integer.parseInt(randConnNumTBox.getText()));
					//settings.setNumNodes(Integer.parseInt(randNodeNumTBox.getText()));
					settings.setNumRandomConnections(Integer.parseInt(randConnNumTBox.getText()));
					settings.setNumNodes(Integer.parseInt(numNodesTBox.getText()));
					settings.setSeed(seedTBox.getText());
				} catch (Exception e){
					Logger.getInstance().log(Logger.WARNING, "SettingsPanel.submit Error: " + e.getMessage());
					e.printStackTrace();
					JOptionPane.showMessageDialog(null, "Error: " + e.getMessage(), "Settings Input Error", JOptionPane.ERROR_MESSAGE);
				} finally {
					thisSettingsPanel.dispose();
				}
			}
		});
		reset.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Logger.getInstance().log(Logger.TRACE, "SettingsPanel.reset click");
				thisSettingsPanel.dispose();
				new SettingsPanel();
			}
		});
		gbc.gridwidth = 1;
		gbc.gridx = 2;
		gbc.gridy = 1;
		gbc.anchor = GridBagConstraints.EAST;
		this.add(reset, gbc);
		
		gbc.gridwidth = 1;
		gbc.gridx = 3;
		gbc.gridy = 1;
		gbc.anchor = GridBagConstraints.EAST;
		this.add(submit, gbc);
		
		gbc.gridx = 5;
		gbc.gridy = 2;
		gbc.weightx = .9;
		gbc.weighty = .9;
		this.add(new JLabel(), gbc);
		this.pack();
		this.setMinimumSize(this.getSize());
		this.setTitle("Meshwork Settings");
		
		this.setVisible(true);
	}
	private void generalSettingsPanel(JTabbedPane tabbedPane){
		//BEGIN GENERAL PANEL
		JPanel randomPanel = new JPanel(new GridBagLayout());

		GridBagConstraints  gbc = new GridBagConstraints();
		tabbedPane.addTab("General", null, randomPanel, "General Settings");

		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.WEST;
		randomPanel.add(new JLabel("Number of Nodes"), gbc);
		numNodesTBox = new JTextField(Integer.toString(Settings.getInstance().getNumNodes()));
		numNodesTBox.setPreferredSize(new Dimension(50,18));
		
		gbc.gridx = 1;
		gbc.gridy = 0;
		randomPanel.add(numNodesTBox, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 1;
		randomPanel.add(new JLabel("Seed"), gbc);
		seedTBox = new JTextField(Settings.getInstance().getSeed());
		seedTBox.setPreferredSize(new Dimension(150,18));

		gbc.gridx = 1;
		gbc.gridy = 1;
		randomPanel.add(seedTBox, gbc);
		//END RANDOM PANEL
		
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 2;
		gbc.gridy = 2;
		randomPanel.add(new JLabel(), gbc);
		randomPanel.setBorder(BorderFactory.createLineBorder(Color.black));
	}
	private void randSettingsPanel(JTabbedPane tabbedPane){
		//BEGIN RANDOM PANEL
		JPanel randomPanel = new JPanel(new GridBagLayout());
		//randomPanel.set
		GridBagConstraints  gbc = new GridBagConstraints();
		tabbedPane.addTab("Random Generation", null, randomPanel, "Random Generation");

		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.WEST;
//		randomPanel.add(new JLabel("Random Nodes"), gbc);
//		randNodeNumTBox = new JTextField(Integer.toString(Settings.getInstance().getNumNodes()));
//		randNodeNumTBox.setPreferredSize(new Dimension(50,18));
//		gbc.weightx = 0;
//		gbc.weighty = 0;
		
		gbc.gridx = 1;
		gbc.gridy = 0;
//		randomPanel.add(randNodeNumTBox, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 1;
		randomPanel.add(new JLabel("Random Connections"), gbc);
		randConnNumTBox = new JTextField(Integer.toString(Settings.getInstance().getNumRandomConnections()));
		randConnNumTBox.setPreferredSize(new Dimension(50,18));

		gbc.gridx = 1;
		gbc.gridy = 1;
		randomPanel.add(randConnNumTBox, gbc);
		//END RANDOM PANEL
//		this.setMinimumSize(new Dimension(300,200));
		
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 2;
		gbc.gridy = 2;
		randomPanel.add(new JLabel(), gbc);
		randomPanel.setBorder(BorderFactory.createLineBorder(Color.black));
	}
}
